package jdbc.homework01;

import java.sql.*;
import java.sql.Date;
import java.util.*;

//2：在控制台输入一个学生的全部信息，保存到数据库，
//注意日期的处理(Statement)
public class StudentMessge {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		Connection con = DbUtil.getConnection();
		Statement st = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			st = con.createStatement();
			System.out.println("请输入学生学号：");
			int stuno = in.nextInt();
			System.out.println("请输入学生姓名：");
			String sname = in.next();
			System.out.println("请输入学生性别：");
			Sex sex = Sex.valueOf(in.next());
			System.out.println("请输入学生电话：");
			String tel = in.next();
			System.out.println("请输入学生QQ：");
			String qq = in.next();
			System.out.println("请输入学生微信：");
			String wechat = in.next();
			System.out.println("请输入学生年级号：");
			int gradeid = in.nextInt();
			System.out.println("请输入学生入学时间：");
			String enterdate = in.next();
			System.out.println("请输入学生城市：");
			String fromcity = in.next();
			System.out.println("请输入学生入学时间：");
			String brithday = in.next();
			System.out.println("请输入学生地址：");
			String address = in.next();
			System.out.println("请输入学生密码：");
			String pass = in.next();
			String sql = "select * from s_student where stuno=" + stuno;
			rs = st.executeQuery(sql);
			if (rs.next()) {
				System.out.println("学生已经存在");
			} else {
				String sql1 = "insert into s_student values(" + stuno + ",'" + sname + "','" + sex + "','" + tel + "','"
						+ qq + "','" + wechat + "'," + gradeid + ",'" + enterdate + "','" + fromcity + "','" + brithday
						+ "','" + address + "','" + pass + "')";
				System.out.println(sql1);
				int r = st.executeUpdate(sql1);
				System.out.println("插入了" + r + "条信息");
			}

		} catch (Exception e) {

			e.printStackTrace();
		} finally {
			DbUtil.close(con, st, rs, ps);
		}
	}

}
